// 游戏对象管理
class GameObjectManager {
    constructor() {
        this.gameObjects = new Map();
        this.localPlayer = null;
    }

    createPlayer(data) {
        if (!data || !data.id) {
            console.error('Invalid player data:', data);
            return null;
        }

        console.log('Creating player:', data);
        try {
            // 如果已存在相同ID的玩家，先移除
            this.removeObject(data.id);

            const player = add([
                sprite("tank"),
                pos(data.x, data.y),
                area(),
                scale(1),
                rotate(data.angle || 0),
                "tank", // 添加标签以便碰撞检测
                {
                    id: data.id,
                    isLocal: data.id === networkManager.getSocketId(),
                    speed: CONFIG.PLAYER.SPEED,
                    health: CONFIG.PLAYER.MAX_HEALTH || 100
                }
            ]);

            if (data.id === networkManager.getSocketId()) {
                player.use(color(...CONFIG.PLAYER.LOCAL_COLOR));
                this.localPlayer = player;
            } else {
                player.use(color(...CONFIG.PLAYER.REMOTE_COLOR));
            }

            this.gameObjects.set(data.id, player);
            return player;
        } catch (error) {
            console.error('Error creating player:', error);
            return null;
        }
    }

    createBullet(data) {
        if (!data || !data.playerId) {
            console.error('Invalid bullet data:', data);
            return null;
        }

        try {
            const bullet = add([
                sprite("bullet"),
                pos(data.x, data.y),
                area(),
                move(data.angle, CONFIG.BULLET.SPEED),
                cleanup(),
                "bullet",
                { 
                    playerId: data.playerId,
                    damage: CONFIG.BULLET.DAMAGE || 10
                }
            ]);

            bullet.onCollide("tank", (tank) => {
                if (tank.id !== bullet.playerId) {
                    networkManager.emitBulletHit({
                        bulletId: data.id,
                        playerId: tank.id,
                        damage: bullet.damage
                    });
                    destroy(bullet);
                }
            });

            return bullet;
        } catch (error) {
            console.error('Error creating bullet:', error);
            return null;
        }
    }

    clear() {
        try {
            this.gameObjects.forEach(obj => {
                if (obj && obj.exists && obj.exists()) {
                    destroy(obj);
                }
            });
            this.gameObjects.clear();
            this.localPlayer = null;
        } catch (error) {
            console.error('Error clearing game objects:', error);
        }
    }

    getLocalPlayer() {
        return this.localPlayer;
    }

    getObject(id) {
        return this.gameObjects.get(id);
    }

    removeObject(id) {
        try {
            const obj = this.gameObjects.get(id);
            if (obj && obj.exists && obj.exists()) {
                destroy(obj);
            }
            this.gameObjects.delete(id);
            
            if (this.localPlayer && this.localPlayer.id === id) {
                this.localPlayer = null;
            }
        } catch (error) {
            console.error('Error removing object:', error);
        }
    }

    updatePlayerHealth(playerId, health) {
        const player = this.getObject(playerId);
        if (player) {
            player.health = health;
        }
    }
}

const gameObjectManager = new GameObjectManager(); 